package Package.Test.Test1;

import Package.Test.Public.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Test1 {
    public static  void Test1(){
        ArrayList<Student> students=new ArrayList<Student>();
        //创建学生对象
        Student s1 = new Student("linqingxia", 30);
        Student s2 = new Student("zhangmanyu", 35);
        Student s3 = new Student("wangzuxian", 33);
        Student s4 = new Student("liuyan", 33);
//把学生添加到集合
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        Collections.sort(students, new Comparator<Student>() {
            @Override
//            比较器实现
            public int compare(Student o1, Student o2) {
               int num=o1.getAge()-o2.getAge();
            int num2=num==0?o1.getName().compareTo(o2.getName()):num;
               return num2;
            }
        });
        for (Student s:students
             ) {
            System.out.println(s.getName()+"----"+s.getAge());
        }
    }
}
